using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Christian.Helle.PocketNews.PocketPC.Controls;
using Christian.Helle.PocketNews.PocketPC.NewsProtocol;
using Christian.Helle.PocketNews.PocketPC.Data;
using System.Threading;

namespace Christian.Helle.PocketNews.PocketPC
{
	public partial class frmMessage : FormEx
	{
		private int index;

		public frmMessage(ArticleHeader header, string body, int index)
		{
			InitializeComponent();
			DisplayDetails(header, body);
			this.index = index;
		}

		private void DisplayDetails(ArticleHeader header, string body)
		{
			lblNewsgroupValue.Text = Configuration.CurrentNewsgroup;
			lblFromValue.Text = header.From;
			lblDateValue.Text = header.Date;
			lblSubjectValue.Text = header.Subject;
			tbMessage.Text = body;
		}

		private void menuClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void menuReply_Click(object sender, EventArgs e)
		{
            var form = new frmCompose(
                lblNewsgroupValue.Text,
                lblSubjectValue.Text,
                tbMessage.Text);

            form.Owner = this;
            if (form.ShowDialog() == DialogResult.OK)
            {
                Close();
            }
		}

		private void menuPrevious_Click(object sender, EventArgs e)
		{
			ReadPrevious();
		}

		private void menuNext_Click(object sender, EventArgs e)
		{
			ReadNext();
		}

		private void ShowStatusBar()
		{
			if (!sbStatus.Visible) {
				sbStatus.Visible = true;
				sbStatus.Update();

				tbMessage.Height -= sbStatus.Height;
			}
		}

		private void HideStatusBar()
		{
			if (sbStatus.Visible) {
				sbStatus.Visible = false;
				sbStatus.Update();

				tbMessage.Height += sbStatus.Height;
			}
		}

		private void UpdateStatusMethod(string text)
		{
			sbStatus.Text = text;
			sbStatus.Update();
		}

		private void ReadPrevious()
		{
			LoadArticle(Cache.ArticleCollection[--index]);
		}

		private void ReadNext()
		{
			LoadArticle(Cache.ArticleCollection[++index]);
		}

		private void LoadArticle(ArticleHeader header)
		{
			if (header != null) {
				try {
					ShowStatusBar();
					UpdateStatusMethod("Retrieving article body");

					string body = null;
					int msg_id = int.Parse(header.ID);

					using (WaitCursor wait = new WaitCursor()) {
						body = DataAccess.Instance.GetArticleBody(
							Configuration.CurrentNewsgroup,
							msg_id);

						if (string.IsNullOrEmpty(body)) {
							using (Nntp nntp = new Nntp()) {
								nntp.Connect(Configuration.NntpServer);
								if (nntp.IsConnected) {
									nntp.SelectGroup(Configuration.CurrentNewsgroup);
									body = nntp.DownloadArticleBody(header.ID);
								}
							}

							if (!string.IsNullOrEmpty(body)) {
								DataAccess.Instance.SaveArticleBody(
									Configuration.CurrentNewsgroup,
									msg_id,
									body);
							} else {
								UpdateStatusMethod("Unable to retrieve body");
								Thread.Sleep(1000);
							}
						}
					}

					DisplayDetails(header, body);
				}
				finally {
					HideStatusBar();
				}
			}
		}

		private void menuMenu_Popup(object sender, EventArgs e)
		{
			menuPrevious.Enabled = index - 1 >= 0;
			menuNext.Enabled = index + 1 < Cache.ArticleCollection.Count;
		}
	}
}